const App = getApp();
const wxParse = require("../../../wxParse/wxParse.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
      num:1,
      detailData:'',
      indicatorDots: true,
      current:0,
    autoplay: false,
    interval: 2000,
    duration: 500,
    color:'#fff',
    colorList:['#FDD1E0','#222222','#D5D9E4','#FAF4F4'],
    active:0,
    desList:[
      {title:'商品描述',contShow:false,content:'时候的股份合计啥地方环境是对方还是地方还是多废话多发的说说'},
      // {title:'产品参数',contShow:false,content:'时候的股份合计啥地方环境是对方还是地方还是多废话多发的说说'},
      // {title:'商品评价',contShow:false,content:'时候的股份合计啥地方环境是对方还是地方还是多废话多发的说说'},
      // {title:'礼品包装',contShow:false,content:'时候的股份合计啥地方环境是对方还是地方还是多废话多发的说说'},
       {title:'支付方式',contShow:false,content:'注意：yc商城微信小程序目前仅能通过微信支付方式进行支付'},
       {title:'客服服务',contShow:false,content:'由微信客服专员为您服务'},
    ],
    isShow:false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let {j_id}=options
      console.log(j_id)
      this.getData(j_id)
  },
  // 图片联动
  selectSwiper(e){
    let index=e.currentTarget.dataset.index
    this.setData({
      current:index
    })
  },
  //立即兑换打开弹窗
  openPop(){
     this.setData({
       isShow:true
     })
  },
  close(){
    this.setData({
      isShow:false
    })
  },
  //立即兑换
  goBuy(e){
    let detail=this.data.detailData
    let spec_attr=this.data.sku&&this.data.sku.spec_attr 
    let goods_id=detail.goods_id
    let goods_num=this.data.num
    var id=""
    if(spec_attr&&spec_attr.length==1){
        id=this.data.id_1
    }else if(spec_attr&&spec_attr.length==2){
        id=this.data.id_1+'_'+this.data.id_2
    }else{
       id=this.data.id_1+'_'+this.data.id_2+'_'+this.data.id_3
    }
    let goods_sku_id=detail.spec_type==10?detail.goods_sku.spec_sku_id+'':id+'';
    console.log(goods_id,goods_num,goods_sku_id)
    if(!goods_sku_id.includes('undefined')){
      wx.setStorageSync('goods_id', goods_id)
      wx.setStorageSync('goods_num', goods_num)
      wx.setStorageSync('goods_sku_id', goods_sku_id)
      wx.navigateTo({
        url: '../sureOrder/index'
      })
    }else{
      wx.showToast({
        title:'请选择规格',
        icon:'none'
      })
    }
  },
  //颜色选择
  selectColor(e){
    console.log(e)
    this.setData({
      active:e.currentTarget.dataset.index
    })
  },
  // 商品描述
  changShow(e){
     let index=e.currentTarget.dataset.index
     let desList=this.data.desList
     desList[index].contShow=!desList[index].contShow
     this.setData({
       desList
     })
  },
  //获取数据
  getData(id){
    var _this=this
      App._get('goods/detail/goods_id/'+id,{},function(res){
        console.log(res)
            let data=res.data
            // 富文本转码
             wxParse.wxParse('content', 'html', data.detail.content, _this, 0);
            let desList=_this.data.desList
            desList[0].content=data.detail.selling_point
            _this.setData({
              desList,
              detailData:data.detail,
              sku:data.specData
            })
      },function(res){
         wx.navigateBack()
      })
  },
  //减
  jian(){
    let num=this.data.num
    if(num<2){
      // App.showSuccess('数量不能小于1')
      wx.showToast({
        title: '数量不能小于1',
        icon:'none'
      })
      return
    }
    num--
    this.setData({
      num:num
    })
  },
  //加
  add(){
     let num=this.data.num
     num++
     this.setData({
       num:num
     })
  },
  //选择规格
  selectSku(e){
    // console.log(e)
    let {id,index,skuindex}=e.currentTarget.dataset
    if(index==0){
      this.setData({
        sku_1:skuindex,
        id_1:id
      })
    }else if(index==1){
      this.setData({
        sku_2:skuindex,
        id_2:id
      })
    }else{
      this.setData({
        sku_3:skuindex,
        id_3:id
      })
    }
    this.changeGoodsInfo()
  },
  //选择规格变化商品信息
  changeGoodsInfo(){
     let spec_list=this.data.sku&&this.data.sku.spec_list
     let spec_attr=this.data.sku&&this.data.sku.spec_attr 
     var id=""
     if(spec_attr.length==1){
       id=this.data.id_1
     }else if(spec_attr.length==1){
       id=this.data.id_1+"_"+this.data.id_2
     }else{
       id=this.data.id_1+"_"+this.data.id_2+"_"+this.data.id_3
     }
     
    //  console.log(id,spec_list)
     let selectInfo=spec_list.find((item)=>{return item.spec_sku_id==id})
     console.log(id,selectInfo)
     if(selectInfo){
       this.setData({
        selectInfo:selectInfo.form
       })
     }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  


  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})